package com.lagou.rocket.demo.controller;

import com.lagou.rocket.demo.enums.OrderStatus;
import com.lagou.rocket.demo.po.Order;
import com.lagou.rocket.demo.service.OrderService;
import com.lagou.rocket.demo.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/order/{userId}/{courseId}")
    public String saveOrder(
            @PathVariable("userId") Integer userId,
            @PathVariable("courseId") Integer courseId
    ) {

        Order order = new Order();
        order.setUserId(userId);
        order.setCourseId(courseId);

        Date date = new Date();

        order.setUpdateTime(date);
        order.setCreateTime(date);

        order.setStatus(OrderStatus.CREATE.getCode());
        String orderNo = IdUtil.genOrderNo();

        order.setOrderNo(orderNo);
        order.setId(IdUtil.genOrderId());

        orderService.saveOrder(order);

        return "redirect:/page/success/" + orderNo;
    }

}
